<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-09-15
 * Time: 22:48
 */

namespace plugin\csauto\app\controller;


use plugin\csauto\app\common\BaseController;
use plugin\csauto\app\model\SystemModel;
use plugin\csauto\app\service\SystemService;
use Shopwwi\LaravelCache\Cache;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class SystemController extends BaseController
{
    /**
     * @var SystemModel
     */
    protected $model = null;

    /**
     * 构造函数
     *
     * @return void
     */
    public function __construct()
    {
        $this->model = new SystemModel;
    }

    public function index(Request $request)
    {
        return view('system/index');
    }

    /**
     * 查询系统配置（过滤readonly配置项）
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request)
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        // 添加过滤条件：不显示 readonly = 1 的配置项
        $where['readonly'] = ['<>', 1];
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }


//    public function insert(Request $request): Response
//    {
//        if ($request->method() === 'POST') {
//            return parent::insert($request);
//        }
//        return view('site/insert');
//    }


    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            if ($request->input('id')) {
                /*当进行设定的时候清理对于的缓存*/
                $systemModel = SystemModel::query()->where('id', $request->input('id'))->first();
                if ($systemModel) {
                    Cache::store('file')->delete(SystemService::getCacheKey($systemModel->key));
                }
            }
            return parent::update($request);
        }
        return view('system/update');
    }

}